<?php

class usercontroller extends CI_Controller
{

    public function __construct()
    {
        parent :: __construct();
        $this->load->model('usermodel');
        $this->load->library('session');
        $this->load->library('encrypt');

    }

    public function validateuser()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'password', 'required');

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('Loginpage');
        }
        else {

            $config['global_xss_filtering'] = TRUE;
            $config['sess_expiration'] = 900;
            $config['sess_expire_on_close'] = TRUE;
            $data['users'] = $this->usermodel->validate();
            if ($data['users'] == NULL) {
                $this->load->view('Loginpage');
            } else {
                $this->session->set_userdata('user_id',$data['users'][0]['user_id']);
                $this->load->view('dashboard');
            }

        }
    }
    public function viewdashboard(){
        if(!$this->session->userdata('user_id'))
        {
            redirect('usercontroller/validateuser','refresh');
        }
        $this->load->helper('form');
        $this->load->view('dashboard');
    }
    public function signout(){
        $this->session->unset_userdata('user_id');
        $this->session->sess_destroy();
        $this->load->helper(array('form', 'url'));
        redirect('usercontroller/validateuser','refresh');
    }

} 